package 我的Java学习_算法基础.day_05;

public class _073_压缩字符串 {
    public static void main(String[] args) {
        String s = "aaabbbccc";
        String n = zipString(s);
        System.out.println(n);
    }
    static String zipString(String src){
        int count = 0;
        char last = 0;
        StringBuilder sb = new StringBuilder();
        for(int i =0;i<src.length();i++){
            char charAt = src.charAt(i);

            if(sb.length()==0){
                sb.append(charAt);
                count++;
            }else{
                if(last==charAt){
                    count++;
                }else{
                    sb.append(count).append(charAt);
                    count=1;
                }
            }
            last = charAt;
        }
        if(count>=1){
            sb.append(count);
        }
        if(sb.length()>=src.length()){
            return src;
        }else{
            return sb.toString();
        }
    }
}
